// 人事行政管理 / 员工管理

import r from '@/utils/request';
import type { ListParams, ListResponseType, ResponseType } from '../types';


enum API {
    list = '/staff/list', //员工列表
    del = '/staff/delete/', //删除员工
    carerDelete = '/staff/carerDelete/',//删除护工
    add = '/staff/add', //添加员工
    careradd = '/staff/careradd',//添加护工
    staffget = '/staff/get/', //根据单条员工
    update = '/staff/update',//修改员工
    delAll = '/staff/deleteAll',//批量删除员工
    UpdateEnable = '/account/updateEnable'
}

export interface UpdateEnableParams {
    id: number;
    enable: number;
}

export function UpdateEnableAPI(params: UpdateEnableParams): Promise<ResponseType<null>> {
    return r.put(API.UpdateEnable, params);
}


export interface staffItem {
    id: number;
}

export interface staffaddparams {
    id: number;
    photo: string;
    name: string;
    mobile: string;
    isCarer: number;
    departmentId: string ;
    departmentName: string;
    roles: Role[];
    adminUserName: string;
    adminPwd: string;
    enable: number;
    idCard: string;
}

export interface Role {
    id: number;
}

export interface stafflistParams extends ListParams {
    name?: string,
    mobile?: string,
    idCard?: string,
    departmentId?: number,
    roleId?: number,
    enable?: number,
    carer?: number
}


export type StaffListResType = {
    id: number;
    companyId: number;
    photo: any | string;
    name: string;
    mobile: any | string;
    isCarer: number;
    departmentId: number;
    addAccountId: number;
    addAccountName: any | string;
    addTime: string | any;
    departmentName: any | string;
    roles: RoleAdd[];
    adminId: number;
    adminUserName: string;
    adminPwd: string;
    enable: number;
    idCard: any | string;
}

export interface RoleAdd {
    id: number;
    name: string;
    companyId: number;
    addAccountId: number;
    addAccountName: any;
    addTime: any;
    accountCounts: number;
    menuIds: any;

}

export interface StaffUpdateParams {
    id: number;
    companyId: number;
    photo: string;
    name: string;
    mobile: string;
    isCarer: number;
    departmentId: string;
    addAccountId: number;
    addAccountName: string;
    addTime: string;
    departmentName: string;
    roles: RoleUpdata[];
    adminId: number;
    adminUserName: string;
    adminPwd: string;
    enable: number;
    idCard: string;
}

export interface RoleUpdata {
    id: number;
    name: string;
    companyId: number;
    addAccountId: number;
    addAccountName: any;
    addTime: any;
    accountCounts: number;
    menuIds: any;
}
type StaffgetResType = ResponseType<{
    id:             number;
    companyId:      number;
    photo:          string;
    name:           string;
    mobile:         string;
    isCarer:        number;
    departmentId:   string;
    addAccountId:   number;
    addAccountName: string;
    addTime:        string;
    departmentName: any;
    roles:          StaffgetListRoles[];
    adminId:        number;
    adminUserName:  string;
    adminPwd:       string;
    enable:         number;
    idCard:         string;
}>

export interface StaffgetListRoles{
    id:             number;
    name:           string;
    companyId:      number;
    addAccountId:   number;
    addAccountName: null;
    addTime:        string;
    accountCounts:  number;
    menuIds:        null;
}

export function staffaddAPI(data: staffaddparams): Promise<ResponseType<null>> {
    return r.post(API.add, data);
}
export function stafflistAPI(params?: stafflistParams): Promise<ListResponseType<StaffListResType>> {
    return r.get(API.list, {...params,});
}
// 护工列表

export function staffCarerlistAPI(params: stafflistParams): Promise<ListResponseType<StaffListResType>> {
    return r.get(API.list, {...params,carer:1});
}

export function DelStaffAPI(id: number): Promise<ResponseType<null>> {
    return r.delete(API.del + id);
}

export function StaffUpdateAPI(params: StaffUpdateParams): Promise<ResponseType<null>> {
    return r.put(API.update, params);
}

export function StaffgetAPI(id: number): Promise<StaffgetResType> {
    return r.get(API.staffget+id);
}

export function CarerAddAPI(id: number[]): Promise<ResponseType<null>> {
    return r.post(API.careradd, id);
}
export function DelCarerAPI(id: number): Promise<ResponseType<null>> {
    return r.delete(API.carerDelete  + id);
}

export function DelCarerAllAPI(ids: number[]){
    ElMessageBox.confirm('确认删除？', {
        cancelButtonText: '取消',
        confirmButtonText: '确认删除'
      }).then(() => {
        Promise.all(
            ids.map((item:number)=> DelCarerAPI(item))
        ).then((arr)=>{
            if(arr.every(item => item.code === 10000)){
                return {
                    code: 10000,
                    message: '删除成功',
                    data:null
                }
            }else {
                return {
                    code: 10001,
                    message: '删除失败',
                    data:null
                }
            }
        })
    })
}
export function DelAllCarerAPI(id: number[]): Promise<ResponseType<null>> {
    return r.post(API.delAll, id);
}